package com.fingersoft.stomp;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;
import java.io.Serializable;

import static android.content.Context.WIFI_SERVICE;

public class AppLaunch {

    public String  type;
    public Double  longitude;//经度
    public Double  latitude;//维度
    public String  networkType;//网络类型
    public String  wifiIdentity;//wifi名称
    public String  wifiMac;//wifi mac地址
    public AppLaunch(Context context,Double longitude,Double latitude){
        this.type="appLaunch";
        this.longitude=longitude;
        this.latitude=latitude;
        this.networkType="网络类型";
        this.wifiIdentity="wifi名称";
        this.wifiMac="wifi mac地址";
        getConnectWifiSsid(context);
        setNetWorkConnectionType(context);

     }

    private void getConnectWifiSsid(Context context) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        Log.d("wifiInfo", wifiInfo.toString());
        Log.d("SSID", wifiInfo.getSSID());
        Log.d("MAC", wifiInfo.getMacAddress());
        this.wifiIdentity=wifiInfo.getSSID();
        this.wifiMac=wifiInfo.getMacAddress();
    }

    /**
     *
     * @return 是否有活动的网络连接
     */
    public final boolean hasNetWorkConnection(Context context){
        //获取连接活动管理器
        final ConnectivityManager connectivityManager= (ConnectivityManager) context.
                getSystemService(Context.CONNECTIVITY_SERVICE);
        //获取链接网络信息
        final NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();

        return (networkInfo!= null && networkInfo.isAvailable());

    }
    /**
     * @return 返回boolean ,是否为wifi网络
     *
     */
    public final boolean hasWifiConnection(Context context)
    {
        final ConnectivityManager connectivityManager= (ConnectivityManager) context.
                getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo networkInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        //是否有网络并且已经连接
        return (networkInfo!=null&& networkInfo.isConnectedOrConnecting());


    }

    /**
     * @return 返回boolean,判断网络是否可用,是否为移动网络
     *
     */

    public final boolean hasGPRSConnection(Context context){
        //获取活动连接管理器
        final ConnectivityManager connectivityManager= (ConnectivityManager) context.
                getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo networkInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        return (networkInfo!=null && networkInfo.isAvailable());

    }
    /**
     * @return  判断网络是否可用，并返回网络类型，ConnectivityManager.TYPE_WIFI，ConnectivityManager.TYPE_MOBILE，不可用返回-1
     */
    public static final int getNetWorkConnectionType(Context context){
        final ConnectivityManager connectivityManager=(ConnectivityManager) context.
                getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo wifiNetworkInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        final NetworkInfo mobileNetworkInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);


        if(wifiNetworkInfo!=null &&wifiNetworkInfo.isAvailable())
        {
            return ConnectivityManager.TYPE_WIFI;
        }
        else if(mobileNetworkInfo!=null &&mobileNetworkInfo.isAvailable())
        {
            return ConnectivityManager.TYPE_MOBILE;
        }
        else {
            return -1;
        }


    }
    private void setNetWorkConnectionType(Context context){
        final ConnectivityManager connectivityManager=(ConnectivityManager) context.
                getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo wifiNetworkInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        final NetworkInfo mobileNetworkInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);


        if(wifiNetworkInfo!=null &&wifiNetworkInfo.isAvailable())
        {
            this.networkType="WIFI";
        }
        else if(mobileNetworkInfo!=null &&mobileNetworkInfo.isAvailable())
        {
            this.networkType="3G/4G";
        }

    }

}
